package com.zr.gdms.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.gdms.dao.LogDao;
import com.zr.gdms.model.Log;
import com.zr.gdms.util.JDBCUtil;

import net.sf.json.JSONObject;

public class LogDaoImpl implements LogDao{

	@Override
	public boolean insertLog(int uid, String uname, String ltime) {
		boolean flag = false;
		StringBuffer  sql = new StringBuffer();
		sql.append(" INSERT INTO log(log.uid,log.uname,log.ltime) VALUES(?,?,?)");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, uid);
			pst.setString(2, uname);
			pst.setString(3, ltime);
			int i = pst.executeUpdate();
			if (i>0) {
				flag = true;
            }
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int getLogCount() {
		int lcount = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(log.lid) AS lcount");
		sql.append(" FROM log");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				lcount = rs.getInt("lcount");
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lcount;
	}

	@Override
	public List<Log> getLog(int page, int rows) {
		List<Log> logs = new ArrayList<Log>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT*FROM log");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, page);
			pst.setInt(2, rows);
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Log l = new Log();
				l.setLid(rs.getInt("lid"));
				l.setUid(rs.getInt("uid"));
				l.setUname(rs.getString("uname"));
				l.setLtime(rs.getString("ltime"));
				logs.add(l);
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return logs;
	}

	@Override
	public List<Log> searchLogByUname(int page, int rows, String uname) {
		List<Log> logs = new ArrayList<Log>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT*FROM log");
		sql.append(" WHERE log.uname=?");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			pst.setInt(2, page);
			pst.setInt(3, rows);
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Log l = new Log();
				l.setLid(rs.getInt("lid"));
				l.setUid(rs.getInt("uid"));
				l.setUname(rs.getString("uname"));
				l.setLtime(rs.getString("ltime"));
				logs.add(l);
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return logs;
	}

	@Override
	public List<Log> searchLogByDate(int page, int rows, String year, String month, String day) {
		List<Log> logs = new ArrayList<Log>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT*FROM log");
		sql.append(" WHERE log.ltime LIKE ? ? ?");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, year);
			pst.setString(2, month);
			pst.setString(3, day+"%");
			pst.setInt(4, page);
			pst.setInt(5, rows);
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Log l = new Log();
				l.setLid(rs.getInt("lid"));
				l.setUid(rs.getInt("uid"));
				l.setUname(rs.getString("uname"));
				l.setLtime(rs.getString("ltime"));
				logs.add(l);
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return logs;
	}

	@Override
	public int getLogCountByUname(String uname) {
		int lcount = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(log.lid) AS lcount");
		sql.append(" FROM log");
		sql.append(" WHERE log.uname=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				lcount = rs.getInt("lcount");
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lcount;
	}

	@Override
	public int getLogCountByDate(String year, String month, String day) {
		int lcount = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(log.lid) AS lcount");
		sql.append(" FROM log");
		sql.append(" WHERE log.ltime LIKE ? ? ?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, year);
			pst.setString(2, month);
			pst.setString(3, day+"%");
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				lcount = rs.getInt("lcount");
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lcount;
	}

}
